package com.example.vaultmysql.dto;

import lombok.Data;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 数据库连接测试结果
 * 
 * @author System
 * @version 1.0.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class DatabaseConnectionTestResult {
    
    /**
     * 测试是否成功
     */
    private boolean success;
    
    /**
     * 测试结果消息
     */
    private String message;
    
    /**
     * 连接延迟（毫秒）
     */
    private long latencyMs;
    
    /**
     * 测试时间戳
     */
    private LocalDateTime timestamp;
    
    /**
     * 数据库名称
     */
    private String databaseName;
    
    /**
     * 错误代码（如果有）
     */
    private String errorCode;
    
    /**
     * 创建成功结果
     */
    public static DatabaseConnectionTestResult success(String message, long latencyMs) {
        return new DatabaseConnectionTestResult(
            true, 
            message, 
            latencyMs, 
            LocalDateTime.now(), 
            null, 
            null
        );
    }
    
    /**
     * 创建成功结果（带数据库名称）
     */
    public static DatabaseConnectionTestResult success(String databaseName, String message, long latencyMs) {
        return new DatabaseConnectionTestResult(
            true, 
            message, 
            latencyMs, 
            LocalDateTime.now(), 
            databaseName, 
            null
        );
    }
    
    /**
     * 创建失败结果
     */
    public static DatabaseConnectionTestResult failure(String message, long latencyMs) {
        return new DatabaseConnectionTestResult(
            false, 
            message, 
            latencyMs, 
            LocalDateTime.now(), 
            null, 
            "CONNECTION_FAILED"
        );
    }
    
    /**
     * 创建失败结果（带数据库名称和错误代码）
     */
    public static DatabaseConnectionTestResult failure(String databaseName, String message, long latencyMs, String errorCode) {
        return new DatabaseConnectionTestResult(
            false, 
            message, 
            latencyMs, 
            LocalDateTime.now(), 
            databaseName, 
            errorCode
        );
    }
    
    /**
     * 获取延迟等级描述
     */
    public String getLatencyLevel() {
        if (latencyMs < 100) {
            return "优秀";
        } else if (latencyMs < 500) {
            return "良好";
        } else if (latencyMs < 1000) {
            return "一般";
        } else if (latencyMs < 3000) {
            return "较慢";
        } else {
            return "很慢";
        }
    }
    
    /**
     * 检查是否成功
     */
    public boolean isSuccess() {
        return success;
    }
    
    /**
     * 检查是否失败
     */
    public boolean isFailure() {
        return !success;
    }
    
    /**
     * 获取格式化的延迟字符串
     */
    public String getFormattedLatency() {
        return latencyMs + "ms (" + getLatencyLevel() + ")";
    }
    
    /**
     * 获取简短的状态描述
     */
    public String getStatusDescription() {
        if (success) {
            return "连接成功 - " + getFormattedLatency();
        } else {
            return "连接失败 - " + message;
        }
    }
    
    @Override
    public String toString() {
        return String.format(
            "DatabaseConnectionTestResult{success=%s, message='%s', latency=%dms, database='%s', timestamp=%s}",
            success, message, latencyMs, databaseName, timestamp
        );
    }
}